package com.zhongwang.mms.module.buy.service;

import com.zhongwang.mms.base.BaseService;
import com.zhongwang.mms.gen.entity.*;
import com.zhongwang.mms.gen.mapper.BShoppingPlanImportLogMapper;
import com.zhongwang.mms.gen.mapper.BShoppingPlanMapper;
import com.zhongwang.mms.module.buy.dao.BuyPlanDao;
import com.zhongwang.mms.module.buy.model.ShoppingPlanModel;
import com.zhongwang.mms.module.system.log.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 采购计划审阅 Service
 *
 * @Author: China.sgl
 * @Date: 2020/4/7 16:25
 */
@SuppressWarnings("ALL")
@Service
public class BuyPlanCheckService{

    @Autowired
    private LogService logService;
    @Autowired
    private BuyPlanDao buyPlanDao;
    @Autowired
    private BShoppingPlanMapper bShoppingPlanMapper;
    @Autowired
    private BShoppingPlanImportLogMapper bShoppingPlanImportLogMapper;

    @Value("${user.upload.path}")
    private String uploadPath;

    /**
     * 查询仓库字典表
     *
     * @return list
     */
    public List<TWarehouseInfo> findStocks() {
        return buyPlanDao.findStocks();
    }

    /**
     * 按条件查询提报计划
     *
     * @param shoppingPlanModel 条件
     * @return list
     */
    public List<ShoppingPlanModel> findBuyPlanByQuery(ShoppingPlanModel shoppingPlanModel) {
//        return buyPlanDao.findExportBuyPlanByQuery(shoppingPlanModel);
        return buyPlanDao.findBuyPlanListByQuery(shoppingPlanModel);
    }

    /**
     * 按ID修改采购计划状态
     *
     * @param id 主键
     * @param status 状态
     * @param desc 日志描述
     * @return 行数
     */
    private int updateBuyPlanStatusById(Integer id, Integer status, String desc) {
        BShoppingPlan plan = bShoppingPlanMapper.selectByPrimaryKey(id);
        logService.saveLog(desc, "项目名：" + plan.getSppProjectName() + "<br>物料描述：" + plan.getSppMatDesc());
        return buyPlanDao.updateReviewFlagById(id, status);
    }

    /**
     * 审批采购计划
     *
     * @param id 主键
     * @return 行数
     */
    public int commitById(Integer id) {
        return updateBuyPlanStatusById(id, 1, "审阅采购计划");
    }

    /**
     * 批量审批采购计划
     *
     * @param ids 主键集合
     * @return 行数
     */
    public int commitInId(Integer[] ids) {
        for (Integer id : ids) {
            commitById(id);
        }
        return 1;
    }

    /**
     * 批量未阅采购计划
     *
     * @param ids 主键集合
     * @return 行数
     */
    public int cancelInId(Integer[] ids) {
        for (Integer id : ids) {
            cancel(id);
        }
        return 1;
    }

    /**
     * 未阅采购计划
     *
     * @param id 主键
     * @return 行数
     */
    public int cancel(Integer id) {
        return updateBuyPlanStatusById(id, null, "未阅 -> 未阅采购计划");
    }
}
